package cn.edu.zxj.rabbitmqmyself.service;

import cn.edu.zxj.rabbitmqmyself.mapper.ProblemMapper;
import cn.edu.zxj.rabbitmqmyself.model.compile.and.run.Request;
import cn.edu.zxj.rabbitmqmyself.model.compile.and.run.Response;
import cn.edu.zxj.rabbitmqmyself.model.compile.and.run.Task;
import cn.edu.zxj.rabbitmqmyself.model.info.ProblemInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description：
 *
 * @author: zxj
 * @date: 2024-10-02
 * @time: 01:58:00
 */
@Service
@Slf4j
public class ProblemService {
    @Autowired
    private ProblemMapper problemMapper;



    public List<ProblemInfo> selectAllProblems() {
        log.info("selectAllProblems");
        return problemMapper.selectAllProblems();
    }

    public ProblemInfo selectProblemById(Integer problemId) {
        log.info("selectProblemById");
        ProblemInfo problemInfo = problemMapper.selectProblemById(problemId);
        problemInfo.setTestCase("");
        return problemInfo;
    }

    public Response compileAndRun(Integer problemId, String userCode) {
        ProblemInfo problemInfo = problemMapper.selectProblemById(problemId);
        if (problemInfo == null) {
            return Response.error("题目不存在");
        }

        // 合并 userCode 和 test_case
        String code = merge(userCode,problemInfo.getTestCase());

        log.info("final code: \n {}", code);

        Request request = new Request();
        request.setCode(code);
        try {
            Task task = new Task();
            return task.compileAndRun(request);
        } catch (IOException e) {
            log.error("编译运行出现异常", e);
        }
        return null;
    }

    private String merge(String userCode, String testCase) {

    /* test_case

    public static void main(String[] args) {
        Solution solution = new Solution();
        // case 1
        int[] ints = solution.twoSum(new int[]{2, 7, 11, 15}, 9);
        if ( ints[0] == 0 && ints[1] == 1) {
            System.out.println("Test pass");
        } else {
            System.out.println("Test failed");
        }
        // case 2
        ints = solution.twoSum(new int[]{3, 2, 4}, 6);
        if ( ints[0] == 1 && ints[1] == 2) {
            System.out.println("Test pass");
        } else {
            System.out.println("Test failed");
        }
        // case 3
        ints = solution.twoSum(new int[]{3, 3}, 6);
        if ( ints[0] == 0 && ints[1] == 1) {
            System.out.println("Test pass");
        } else {
            System.out.println("Test failed");
        }
    }

    userCode:
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] result = new int[2];
            for (int i = 0; i < nums.length; i++) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] + nums[j] == target) {
                        result[0] = i;
                        result[1] = j;
                        return result;
                    }
                }
            }
            return result;
        }
    }

    最终合并结果code :
    class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] result = new int[2];
            for (int i = 0; i < nums.length; i++) {
                for (int j = i + 1; j < nums.length; j++) {
                    if (nums[i] + nums[j] == target) {
                        result[0] = i;
                        result[1] = j;
                        return result;
                    }
                }
            }
            return result;
        }
        public static void main(String[] args) {
            Solution solution = new Solution();
            // case 1
            int[] ints = solution.twoSum(new int[]{2, 7, 11, 15}, 9);
            if ( ints[0] == 0 && ints[1] == 1) {
                System.out.println("Test pass");
            } else {
                System.out.println("Test failed");
            }
            // case 2
            ints = solution.twoSum(new int[]{3, 2, 4}, 6);
            if ( ints[0] == 1 && ints[1] == 2) {
                System.out.println("Test pass");
            } else {
                System.out.println("Test failed");
            }
            // case 3
            ints = solution.twoSum(new int[]{3, 3}, 6);
            if ( ints[0] == 0 && ints[1] == 1) {
                System.out.println("Test pass");
            } else {
                System.out.println("Test failed");
            }
        }
    }
    */
        StringBuilder sb = new StringBuilder();
        sb.append(userCode, 0, userCode.lastIndexOf("}"));
        sb.append(testCase);
        sb.append("}");
        return sb.toString();
    }
}
